// pages/news/news.js
//获取应用实例
var app = getApp();
var util = require('../../utils/util.js');
// index/list.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        tabbar: {},
        select: false,
        tihuoWay: '全部',
        orderData: [],
        pushData: [],
        pageSize: 15,
        pageNo: 1,
        title: "下拉查看更多",
        token: "",
        abort: true
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.setData({
            token: wx.getStorageSync('token')
        })
        let token = wx.getStorageSync('token')
        if (!token) {
            wx.showModal({
                title: '提示',
                content: '请先登录后查看',
                showCancel: true,//是否显示取消按钮
                cancelText: "取消",//默认是“取消”
                confirmText: "去登录",
                success: function (res) {
                    if (res.cancel) {
                        //点击取消,默认隐藏弹框
                        wx.switchTab({
                            url: '/pages/index/index',
                        })
                    } else {
                        //点击确定
                        wx.redirectTo({
                            url: '/pages/account/login',
                        })
                    }
                },
            })
        } else {
            this.setData({
                pageNo: 1,
                orderData: []
            })
            let that = this
            let pageNo = that.data.pageNo
            let pageSize = that.data.pageSize
            that.getIndexData(pageNo, pageSize);
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // util.isLoginModal();
        app.editTabbar();
        var that = this;
        that.setData({
            token: wx.getStorageSync('token')
        })
        // let token = wx.getStorageSync('token')
        // if (token) {
        //     let pageNo = 1
        //     let pageSize = that.data.pageSize
        //     that.getIndexData(pageNo, pageSize);
        // }
    },
    // 下拉请求数据
    more: function () {
        this.onReachBottom()
    },
    onPullDownRefresh() {
        wx.showNavigationBarLoading();
        this.setData({
            pageNo: 1,
            orderData: [],
            abort: true
        })
        let pageNo = this.data.pageNo
        let pageSize = this.data.pageSize
        this.getIndexData(pageNo, pageSize);
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
    },
    // 时间  
    bindShowMsg() {
        this.setData({
            select: !this.data.select,
            selectone: false
        })
    },
    mySelect(e) {
        var name = e.currentTarget.dataset.name
        this.setData({
            tihuoWay: name,
            select: false
        })
    },
    // 分享
    onShareAppMessage: function (res) {
        var that = this;
        return {
            title: '贷投狗小程序',
            path: 'pages/referenceprice/referenceprice',
            success: function (res) {
                that.shareClick();
            },
            fail: function (res) {
            }
        }
    },
    // 退出登录
    logout: function () {
        wx.showModal({
            title: '提示',
            content: '是否确认退出',
            cancelText: '取消',
            confirmText: '退出',
            success(res) {
                if (res.cancel) {
                    // 用户点击了取消属性的按钮
                } else if (res.confirm) {
                    // 用户点击了确定属性的按钮
                    wx.reLaunch({
                        url: '/pages/account/login',
                    })
                    util.https(app.globalData.api + "/applet/income/logout", "POST", {},
                        function (data) {
                            if (data.code == 200) {
                                wx.removeStorageSync("orderNumber");
                                wx.removeStorageSync("orderStatus");
                                wx.removeStorageSync("productId");
                                wx.removeStorageSync("token");
                                wx.removeStorageSync("teamPartnerStatus");
                                wx.removeStorageSync("empClass");
                                wx.removeStorageSync("isJoinTeam");
                                wx.removeStorageSync("projectModeLength");
                                // wx.reLaunch({
                                //   url: '/pages/account/login',
                                // })
                            } else {
                                wx.showToast({
                                    title: '请求失败',
                                })
                            }
                        }
                    )
                }
            }
        })
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    // 上拉刷新
    onScrollToEnd() {
        //做些什么
        wx.showNavigationBarLoading()
        var that = this;
        let pageNo = that.data.pageNo + 1
        this.setData({
            pageNo: pageNo
        })
        let pageSize = that.data.pageSize 
        if (that.data.abort == true) {
            that.getIndexData(pageNo, pageSize);
        }
    },
    // 请求数据
    getIndexData: function (pageNo, pageSize) {
        let token = wx.getStorageSync('token')
        if (token) {
            var that = this;
            //订单查询
            util.https(app.globalData.api + "/applet/income/order", "GET", { pageNo: pageNo, pageSize: pageSize },
                function (data) {
                    if (data.code == 200) {
                        let Data = that.data.orderData.concat(data.data)
                        console.log(Data, "data")
                        // let dataList = Data.filter(item => item.id == item.id)
                        that.setData({
                            orderData: Data
                        })
                        if (data.data.length == 0) {
                            that.setData({
                                title: '暂无更多数据',
                                abort: false
                            })
                        } else {
                            that.setData({
                                title: '下拉查看更多',
                            })
                        }
                    } else {
                        util.toolTip(that, data.message)
                    }
                }
            )
            //订单查询
            util.https(app.globalData.api + "/applet/income/count", "GET", {},
                function (data) {
                    if (data.code == 200) {
                        that.setData({
                            countData: data.data
                        })
                    } else {
                        util.toolTip(that, data.message)
                    }
                }
            )
        } else {
            this.setData({
                title: "登录查看数据"
            })
        }
    },
})